package com.sneaker.shower.controller;

import com.sneaker.shower.common.BaseView;
import com.sneaker.shower.entity.dto.PayOrderDTO;
import com.sneaker.shower.entity.dto.PrepareToPayReqDTO;
import com.sneaker.shower.entity.dto.TryPayRespDTO;
import com.sneaker.shower.service.SubOrderService;
import com.sneaker.shower.util.JacksonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "订单相关API")
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    private final SubOrderService subOrderService;


    @Autowired
    public OrderController(SubOrderService subOrderService) {
        this.subOrderService = subOrderService;
    }

    @PostMapping("/try_pay")
    @ApiOperation(value = "尝试支付，判断返回的payOrderDTO.status是否等于1，如果已经等于1，则表示支付成功，否则需使用wxPayParamDTO去进行小程序支付")
    public BaseView<TryPayRespDTO> prepareToPay(@ApiParam("使用会员卡支付时需要") @RequestBody PrepareToPayReqDTO prepareToPayReqDTO) {
        log.info("try_pay请求入参：{}", JacksonUtils.writeValueAsString(prepareToPayReqDTO));
        TryPayRespDTO tryPayRespDTO = subOrderService.tryPay(prepareToPayReqDTO);
        log.info("try_pay 返回结果：{}", JacksonUtils.writeValueAsString(tryPayRespDTO));
        return new BaseView<>(tryPayRespDTO);
    }

    @PostMapping("/offline_try_pay")
    @ApiOperation(value = "线下尝试支付，判断返回的payOrderDTO.status是否等于1，如果已经等于1，则表示支付成功，否则需使用wxPayParamDTO去进行小程序支付")
    public BaseView<TryPayRespDTO> offlineTryPay(@RequestBody PrepareToPayReqDTO prepareToPayReqDTO) {
        log.info("offline_try_pay请求入参：{}", JacksonUtils.writeValueAsString(prepareToPayReqDTO));
        return new BaseView<>(subOrderService.offlineTryPay(prepareToPayReqDTO));
    }

    @GetMapping("/polling")
    @ApiOperation("订单支付轮询")
    public BaseView<PayOrderDTO> polling(String payGuid) {
        return new BaseView<>(subOrderService.polling(payGuid));
    }
}
